<?php
class PageController extends CController
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			)
		);
	}

	public function actionIndex()
	{
		if (!defined('CRYPT_BLOWFISH')||!CRYPT_BLOWFISH)
			throw new CHttpException(500,"This application requires that PHP was compiled with Blowfish support for crypt().");
		$model=new User;
		$model->scenario = 'withCaptcha';

		//Nếu chưa đăng nhập thì hiển thị login
		if (Yii::app()->user->getIsGuest())
			$this->render('index',array('model'=>$model));
		else
			$this->redirect(Yii::app()->user->returnUrl);
	}
	public function actionLostPassword()
	{
		if (!Yii::app()->user->getIsGuest())
			$this->redirect(Yii::app()->user->returnUrl);
		$this->render('password');
	}


	public function actionFeed($id = 'me')
	{
		if (Yii::app()->user->getIsGuest())
			$this->redirect(Yii::app()->request->baseUrl);
		else
		{
			$idUser = Yii::app()->user->getId();

			//Get user information
			$content = ($id == 'me' ?  ListmusicController::getContentFeed($idUser): ListmusicController::getContentFeed($id,$idUser));

			$this->renderFeed('feed',$id,$content);
		}
	}

	public function actionNews()
	{
		if (Yii::app()->user->getIsGuest())
			$this->redirect(Yii::app()->request->baseUrl);
		else
		{
			$idUser = Yii::app()->user->getId();

			//Get content news
			$content = ListmusicController::getContentNew($idUser);
			$this->renderNews('news',$content);
		}
	}

	public function actionChangeAvatar()
	{
		if (Yii::app()->user->getIsGuest())
			$this->redirect(Yii::app()->request->baseUrl);
		else
		{
			$idUser = Yii::app()->user->getId();

			$this->renderFeed('change-avatar');
			//$this->render('change-avatar');
		}
	}


	public function actionAddList()
	{

	}

	public function actionList()
	{

	}

	public function actionMusic()
	{

	}

	public function actionMessenger()
	{

	}

	public function actionInformation()
	{

	}

	public function actionFindFriend()
	{

	}

	public function actionListFriend()
	{

	}

	private function renderFeed($view,$id='me',$content="",$return=false)
	{
		if($this->beforeRender($view))
		{

			$idUser = Yii::app()->user->getId();

			//Get user information
			$data['user'] = ($id == 'me' ?  UserController::view($idUser): UserController::view($id));

			//Get 6 friend
			$data['friends'] = ($id == 'me' ?  FriendshipController::getFriend6($idUser): FriendshipController::getFriend6($id));

			//Get 6 list music
			$data['lists'] = ($id == 'me' ?  ListmusicController::getList5($idUser): ListmusicController::getList5($id,$idUser));

			//Get online
			$data['messenger'] = MessengerController::getFriendsOnline($idUser);

			//Get content page
			$data['content'] = $content;

			$data['user-page'] = $id;


			$contentLeft    = $this->renderPartial('_aside-feed',array('data'=>$data),true);
			$content        = $this->renderPartial($view,array('data'=>$data),true);
			$contentRight   = $this->renderPartial('_aside-chat',array('data'=>$data),true);
			$coverPhoto     = $this->renderPartial('_content-cover',array('data'=>$data),true);

			$this->layout   = 'column-feed';

			if(($layoutFile = $this->getLayoutFile($this->layout))!==false)
			$output         = $this->renderFile($layoutFile,array(
			'contentLeft'   =>$contentLeft,
			'content'       =>$content,
			'contentRight'  =>$contentRight,
			'coverPhoto'    =>$coverPhoto
			),true);

			$this->afterRender($view,$output);

			$output         = $this->processOutput($output);

			if($return)
				return $output;
			else
				echo $output;
		}
	}

	private function renderNews($view,$content=null,$return=false)
	{
		if($this->beforeRender($view))
		{
			$idUser = Yii::app()->user->getId();

			//Get online
			$data['messenger'] = MessengerController::getFriendsOnline($idUser);

			//Get list friends can know
			$data['friends'] = FriendshipController::friends($idUser);

			//Get list requesting friend
			$data['listRequesting'] = FriendshipController::listRequesting($idUser);

			//Get list favorite
			$data['listFavorite'] = ListmusicController::getListFavorite();

			//Get list random
			$data['listRandom'] = ListmusicController::getListRandom();

			//Get content page
			$data['content'] = $content;

			$contentLeft    = $this->renderPartial('_aside-news',array('data'=>$data),true);
			$content        = $this->renderPartial($view,array('data'=>$data),true);
			$contentRight   = $this->renderPartial('_aside-chat',array('data'=>$data),true);


			$this->layout   = 'column3';

			if(($layoutFile = $this->getLayoutFile($this->layout))!==false)
			$output         = $this->renderFile($layoutFile,array(
			'contentLeft'   =>$contentLeft,
			'content'       =>$content,
			'contentRight'  =>$contentRight
			),true);

			$this->afterRender($view,$output);

			$output         = $this->processOutput($output);

			if($return)
				return $output;
			else
				echo $output;
		}
	}

	private function renderPage($view,$data=null,$return=false)
	{
		if($this->beforeRender($view))
		{
			$content        = $this->renderPartial($view,$data,true);
			$contentRight   = $this->renderPartial('_aside-chat',$data,true);

			$this->layout   = 'column2';

			if(($layoutFile = $this->getLayoutFile($this->layout))!==false)
			$output         = $this->renderFile($layoutFile,array(
			'content'       =>$content,
			'contentRight'  =>$contentRight,
			),true);

			$this->afterRender($view,$output);

			$output         = $this->processOutput($output);

			if($return)
				return $output;
			else
				echo $output;
		}
	}
}
?>